<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    public $view;
    public $model;
    public $title;
    public $baseModel;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $title = $this->title;
        return view('backend.'.$this->view.'.index',compact('title'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('backend.'.$this->view.'.create');
    }

    public function validateStore($request)
    {
        $request->validate([
            'name' => 'required',
        ]);

        $data = $request->all();
        return $data;
    }

    public function validateUpdate($request)
    {
        return $this->validateStore($request);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $this->validateStore($request);
        $this->baseModel::create($data);
        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function showAll()
    {
        $data = request()->all();
        $total = $this->baseModel::count();
        $data = $this->baseModel::skip($data['offset'])->limit($data['limit'])->get();

        return response()->json([
            'status' => 0,
            'data' => [
                'data' => $data,
                'total' => $total
            ]
        ]);
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function edit($model)
    {
        return view('backend.'.$this->view.'.update',compact($model));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $model)
    {
        $data = $this->validateUpdate($request);
        $model->update($data);
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function destroy($model)
    {
        $model->delete();
        return redirect()->back();
    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function batchDelete()
    {
        $ids = request()->get('batch-delete');
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            $model = $this->baseModel::find($id);
            $model->delete();
        }
        return redirect()->back();
    }
}
